package com.hymjweb.demo.hymj.basic_data_manage.xt_pick_up_point.service;

import com.alibaba.fastjson.JSONObject;
import com.hymjweb.demo.framework.cache.service.GlobalCacheService;
import com.hymjweb.demo.framework.result.MIniUIGridResultMap;
import com.hymjweb.demo.framework.sys.BaseService;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_pick_up_point.bean.XtPickUpPointPojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_pick_up_point.mappper.XtPickUpPointMapper;
import com.hymjweb.demo.hymj.basic_data_manage.xt_upload_file.bean.XtUploadFilePojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_upload_file.service.XtUploadFileService;
import com.hymjweb.demo.hymj.log_manage.aop.annotation.OperationLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class XtPickUpPointService extends BaseService {
    @Autowired
    private XtPickUpPointMapper xtPickUpPointMapper;

    @Autowired
    private GlobalCacheService globalCacheService;

    @Autowired
    private XtUploadFileService xtUploadFileService;


    public XtPickUpPointPojo convertToJavaBean(JSONObject xtPickUpPointJsonObj, String opFlag) throws Exception {
        if(xtPickUpPointJsonObj==null){
            throwException("提货点对象不存，转换失败!");
        }

        XtPickUpPointPojo pojo = xtPickUpPointJsonObj.toJavaObject(XtPickUpPointPojo.class);

        Date currentTime = this.getSysTime();
        String czyId = this.getLoginCzyId();
        if (opFlag.equals(SysConstant.OPER_ADD)) {
            // 设置 ID
            pojo.setId(this.getSysStrId());
            pojo.setCreateTime(currentTime);
            pojo.setCreateUserId(czyId);
        }
        pojo.setUpdateTime(currentTime);
        pojo.setUpdateUserId(czyId);

        return pojo;
    }

    public List<XtPickUpPointPojo> findCacheAll(Map map) {
        return xtPickUpPointMapper.findAll(map);
    }

    public Object getPageList(Map map) {
        this.pageListInit(map);
        List list = getNoPageList(map);
        MIniUIGridResultMap resultMap = this.pageToGrid(list);
        return resultMap;
    }

    public List<XtPickUpPointPojo> getNoPageList(Map map) {
        return xtPickUpPointMapper.findAll(map);
    }

    /**
     * 根据条件获取提货点列表，主要供开主单时使用
     *
     * @param key
     * @param cityId
     * @return
     */
    public List<XtPickUpPointPojo> queryPickUpPointByCondition(String key, String cityId) {
        List<XtPickUpPointPojo> list = xtPickUpPointMapper.queryPickUpPointByCondition(key, cityId);
        return list;
    }

    /**
     * 删除提货点对象
     *
     * @param pojo
     * @return
     * @throws Exception
     */
    @OperationLogger(oper = SysConstant.OPER_ADD, operTitle = "提货点-新增", operMemo = "", classType = XtPickUpPointPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int insert(XtPickUpPointPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("提货点新增参数错误,新增失败!");
        }
        // 检测UK字段不能为空
        checkPojo(pojo);

        // 检测是否有UK重复
        XtPickUpPointPojo pojo1 = xtPickUpPointMapper.queryPojoByUk(pojo);

        if (pojo1 != null) {
            throwException("根据【类型】【目的站】【提货点名称】检测，系统中已经存在该提货点，不能重复添加!");
        }

        if (DataTypeUtil.isEmptyStr(pojo.getFileId())) {
            pojo.setFileId(null);
        }

        int affectRows = xtPickUpPointMapper.insert(pojo);
        if (affectRows > SysConstant.INT_ZERO) {
            globalCacheService.loadXtPickUpPointCache();
        }
        return affectRows;

    }

    /**
     * 更新提货点对象
     *
     * @param pojo
     * @return
     * @throws Exception
     */
    @OperationLogger(oper = SysConstant.OPER_UPDATE, operTitle = "提货点-更新", operMemo = "", classType = XtPickUpPointPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int update(XtPickUpPointPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("提货点更新参数错误,更新失败!");
        }

        checkPojo(pojo);

        if (DataTypeUtil.isEmptyStr(pojo.getFileId())) {
            pojo.setFileId(null);
        }
        int affectRows = xtPickUpPointMapper.update(pojo);
        if (affectRows > SysConstant.INT_ZERO) {
            globalCacheService.loadXtPickUpPointCache();
        }
        return affectRows;
    }

    /**
     * 批量删除,ids 格式为 'id1','id2','id3'
     *
     * @param ids
     * @return
     * @throws Exception
     */
    @OperationLogger(oper = SysConstant.OPER_DELETE, operTitle = "提货点-删除", operMemo = "", classType = XtPickUpPointPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int delete(String ids) throws Exception {
        if (DataTypeUtil.isEmptyStr(ids)) {
            throwException("提货点删除ID参数错误,删除失败!");
        }

        // 1. 先根据 ids查询到所有要删除的提货点对象
        List<XtPickUpPointPojo> pojos = xtPickUpPointMapper.getXtPickUpPointPojoListByIds(ids);

        if (pojos == null || pojos.size() == 0) {
            throwException("待删除提货点在不存在，删除失败!");
        }

        // 删除文件
        for (XtPickUpPointPojo pojo : pojos) {
            String fileId = pojo.getFileId();
            if (fileId != null) {
                XtUploadFilePojo filePojo = xtUploadFileService.findObjectById(fileId);
                xtUploadFileService.deleteFile(filePojo);
            }
        }

        // 删除提货点
        int affectRows = xtPickUpPointMapper.delete(ids);
        if (affectRows > SysConstant.INT_ZERO) {
            globalCacheService.loadXtPickUpPointCache();
        }
        return affectRows;
    }

    /**
     * 检查必输项
     *
     * @param pojo
     */
    private void checkPojo(XtPickUpPointPojo pojo) throws Exception {
        DataTypeUtil.isEmptyStrThenThrow(pojo.getId(), SysConstant.XT_DIC_ERROR_MSG.no_id.getName());
        DataTypeUtil.isEmptyStrThenThrow(pojo.getName(), SysConstant.XT_DIC_ERROR_MSG.no_name.getName());
        DataTypeUtil.isEmptyStrThenThrow(pojo.getPickUpTypeId(), "提货点类型为空，不能保存");
        DataTypeUtil.isEmptyStrThenThrow(pojo.getCityId(), SysConstant.XT_DIC_ERROR_MSG.no_city.getName());
    }



}
